package cn.hniu.studentmanager.controller;

import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserController {
    Map<String, String> userMap; //定义用户map存储账号密码
    Map<String, Map<String, String>> studentMap; //存储学生信息

    //构造方法
    public UserController() {
        userMap = new HashMap<>();
        userMap.put("admin", "123456");
        userMap.put("hniu", "123");

        studentMap = new HashMap<>();
    }

    //通过以下地址访问helo123接口
    //http://localhost:8090/user/hello123
    //http://127.0.0.1:8090/user/hello123
    @GetMapping("hello123")
    public String hello() {
        return "hello world";
    }

    //通过以下地址访问接口：
    //http://localhost:8090/user/login?u=123&p=123
    //http://127.0.0.1:8090/user/login?u=123&p=123
    @GetMapping("login")
    public String login(@RequestParam("u") String user, @RequestParam("p") String password,@RequestParam(name = "role",defaultValue = "0") Integer role) {

        if (role == 0 && userMap.get(user) == null ) {
            return "找不到用户：" + user;
        } else if(role == 1 && studentMap.get(user) == null){
            return "找不到用户：" + user;
        } else {
            //判断登录角色
            if(role == 1){
                Map<String,String> stuInfo = studentMap.get(user);
                if(password.equals(stuInfo.get("password"))){
                    return user;
                }
                return "密码错误：" + user;
            }
            //判断账号密码是否一致
            String _password = userMap.get(user);
            if (_password.equals(password)) {
                return user;
            }
        }

        return "密码错误：" + user;
    }

    //http://127.0.0.1:8090/user/add
    //http://localhost:8090/user/add?no=1001&name=张三&sex=男&dept=软件学院&cls=移动应用&age=15
    @GetMapping("add")
    public Map<String, String> addUser(@RequestParam("no") String no,
                                       @RequestParam("name") String name,
                                       @RequestParam(value = "sex", required = false) String sex,
                                       @RequestParam(value = "age", required = false) String age,
                                       @RequestParam(value = "dept", required = false) String dept,
                                       @RequestParam(value = "cls", required = false) String cls,
                                       @RequestParam(value = "password",required = true) String password) {
        Map<String, String> studentInfo = new HashMap<>();

        if (Strings.isBlank(no) || Strings.isBlank(name)) {
            studentInfo.put("no", "0");
            studentInfo.put("msg", "学号或姓名不能为空");
            return studentInfo;
        }

        if (studentMap.get(no) != null) {
            studentInfo.put("no", "0");
            studentInfo.put("msg", "已经添加该生数据，学号：" + no);
            return studentInfo;
        }

        studentInfo.put("no", no); //姓名
        studentInfo.put("name", name); //姓名
        studentInfo.put("sex", sex); //性别
        studentInfo.put("age", age); //年龄
        studentInfo.put("dept", dept); //学院
        studentInfo.put("cls", cls); //班级
        studentInfo.put("password", password); //班级
        studentMap.put(no, studentInfo);

        return studentInfo;
    }

    //查询指定学号的接口
    //http://localhost:8090/user/query?no=1001
    @GetMapping("query")
    public Map<String, String> query(@RequestParam("no") String no) {
        if (studentMap.get(no) == null) {
            return new HashMap<>();
        }
        return studentMap.get(no);
    }

    //查询接口
    //http://127.0.0.1:8090/user/queryAll
    @GetMapping("queryAll")
    public List<Map<String, String>> query() {
        List<Map<String, String>> list = new ArrayList<>();
        for (String key : studentMap.keySet()) {//遍历所有的学生
            list.add(studentMap.get(key));
        }
        return list;
    }

    //删除接口
    //http://localhost:8090/user/delete?no=1001
    @GetMapping("delete")
    public String deleteByNo(@RequestParam("no") String no) {
        //前提是有数据才能删除
        if (studentMap.containsKey(no)) {
            studentMap.remove(no);
            return no;
        }else{
            return "删除不存在的学号：" + no;
        }
    }
}
